/**
 * SysUserController.java -- 2017-10-19 19:47:48
 * 作者：欧增奇 -- 代码生成器自动生成
 * 联系方式：425752797@qq.com
 * http://www.ouzengqi.com/
 */
package com.ozq.sys.controller.action;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ozq.common.dao.entity.SpringJson;
import com.ozq.common.page.Page;
import com.ozq.common.validator.Jump;
import com.ozq.common.validator.Update;
import com.ozq.sys.controller.form.SysUserForm;
import com.ozq.sys.dao.entity.SysUser;
import com.ozq.sys.service.inter.SysUserService;

/**
 * <系统-用户表控制> <功能详细描述>
 * 
 * @author 欧增奇
 * @version [V2.0, 2017年10月19日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@RequestMapping("sys")
public class SysUserController
{
    static Logger logger = LoggerFactory.getLogger(SysUserController.class);

    /**
     * 应用-系统-用户表服务
     */
    @Autowired
    private SysUserService sysUserService;

    /**
     * 应用-系统-用户表-跳转
     * 
     * @param sysUserForm
     * @return
     */
    @RequestMapping("jump/sysUser/to")
    public String jumpTo(Model model, @Validated({ Jump.class }) SysUserForm sysUserForm, BindingResult result)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysUserForm:{}", sysUserForm);
        }
        
        // 参数验证-不合法直接返回错误的页面或则错误的json
        if (result.hasErrors())
        {
            model.addAttribute("errors", result.getFieldErrors());
            return "common/error";
        }
        
        return "sysUser/" + sysUserForm.getJump();
    }

    /**
     * 应用-系统-用户表分页查询列表-执行
     * 
     * @param model
     * @param sysUserForm
     * @return
     */
    @RequestMapping("listPage/sysUser/do")
    public String listPageSysUserDo(Model model, SysUserForm sysUserForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysUserForm:{}", sysUserForm);
        }

        // 基本类
        SysUser sysUser = sysUserForm.getSysUser();
        if (sysUser == null)
            sysUser = new SysUser();

        // 模糊统计
        int totalCount = sysUserService.countLike(sysUser);

        // 创建分页
        Page page = sysUserForm.getPage();
        if (page == null)
            page = new Page();
        page.setTotalCount(totalCount);
        page = sysUserService.pageManage(page);

        // 模糊分页查询
        model.addAttribute("sysUsers", sysUserService.listPageLike(page, sysUser));
        model.addAttribute("page", page);
        model.addAttribute("sysUser", sysUser);
        return "sysUser/listPageSysUser";
    }
	
	/**
     * 应用-系统-用户表分页查询列表-执行-异步
     * 
     * @param model
     * @param sysUserForm
     * @return
     */
	@ResponseBody
    @RequestMapping(value = "ajaxListPage/sysUser/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson ajaxListPageSysUserDo(Model model, SysUserForm sysUserForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysUserForm:{}", sysUserForm);
        }

        // 基本类
        SysUser sysUser = sysUserForm.getSysUser();
        if (sysUser == null)
            sysUser = new SysUser();

        // 模糊统计
        int totalCount = sysUserService.countLike(sysUser);

        // 创建分页
        Page page = sysUserForm.getPage();
        if (page == null)
            page = new Page();
        page.setTotalCount(totalCount);
        page = sysUserService.pageManage(page);

		// 模糊分页查询
        Map<String, Object> map = new HashMap<>();
        map.put("sysUsers", sysUserService.listPageLike(page, sysUser));
        map.put("page", page);
        map.put("sysUser", sysUser);
        return new SpringJson(true, map);
    }
	
	/**
     * 应用-系统-用户表分页查询列表-执行-异步
     * 
     * @param model
     * @param sysUserForm
     * @return
     */
	@ResponseBody
    @RequestMapping(value = "ajaxList/sysUser/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson ajaxListSysUserDo(Model model, SysUserForm sysUserForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysUserForm:{}", sysUserForm);
        }

        // 基本类
        SysUser sysUser = sysUserForm.getSysUser();
        if (sysUser == null)
            sysUser = new SysUser();

        return new SpringJson(true, sysUserService.listLike(sysUser));
    }

    /**
     * 应用-系统-用户表修改跳转-执行
     * 
     * @param model
     * @param sysUserForm
     * @return
     */
    @RequestMapping("update/sysUser/to")
    public String updateSysUserTo(Model model, @Validated({ Update.class }) SysUserForm sysUserForm, BindingResult result)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysUserForm:{}", sysUserForm);
        }

        // 参数验证-不合法直接返回错误的页面或则错误的json
        if (result.hasErrors())
        {
            model.addAttribute("errors", result.getFieldErrors());
            return "common/error";
        }

        model.addAttribute("sysUser", sysUserService.findById(sysUserForm.getId()));
        return "sysUser/addOrUpdateSysUser";
    }
    
    /**
     * 应用-系统-用户表修改-执行
     * 
     * @param model
     * @param sysUserForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "update/sysUser/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson updateSysUserDo(Model model, SysUserForm sysUserForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysUserForm:{}", sysUserForm);
        }

        boolean result = sysUserService.update(sysUserForm.getSysUser());
        return new SpringJson(result);
    }
	
    /**
     * 应用-系统-用户表添加-执行
     * 
     * @param model
     * @param sysArticleForm
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "add/sysUser/do", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public SpringJson addSysUserDo(Model model, SysUserForm sysUserForm)
    {
        if (logger.isDebugEnabled())
        {
            logger.debug("debug sysUserForm:{}", sysUserForm);
        }

        SysUser sysUser = sysUserService.add(sysUserForm.getSysUser());
        return new SpringJson(sysUser == null ? false : true);
    }
}